package com.sptpc.demo1.controller;

import com.sptpc.demo1.controller.requests.AddBookRequest;
import com.sptpc.demo1.model.Book;
import com.sptpc.demo1.service.IBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;


@Slf4j  //日志
@RestController
@RequestMapping("/book")
public class BookController {
    //调入有实现方法的接口，实现方法注入了Repository
    @Autowired
    IBookService bookService;

    @GetMapping("/getall")
    public List<Book> getAll(){
        //分页显示
//        PageRequest pageRequest= PageRequest.of(page-1,limit);
        //数据库查询数据
        List<Book> results=bookService.getAllDrinks();
        return results;
    }

    /**
     * 查询指定id
     * get/drink/1  (path表示法)get /drink/
     * @return
     */
//    @GetMapping("/{id}")
//    public Book getDrink(@PathVariable Integer id){
//        log.info("查询饮品的id: {}",id);
//        //  后期从数据库查询
//        Book book =(Book.builder().id(1)
//                .name("茉莉花茶")
//                .build());
//        return book;
//    }

    /**
     * 新增
     * post  /drink/{"name": "奶茶","price": "8.00",“categoryid”:"1"}
     * 1. 直接使用model类接受用户参数
     * 2. 编写一个TO类专门负责传值
     */
    @PostMapping("/add")
    public Book addNewBook(@RequestBody AddBookRequest book, BindingResult bindingResult){
        log.info("创建新饮品: {}", book);
        Book saveBook = new Book();
        BeanUtils.copyProperties(book,saveBook);
        bookService.addNewDrink(saveBook);
        return saveBook;
    }

    /**
     * 修改
     * put /drink/{"id": 1, "name": "新奶茶", "price": "10.0"}
     */
    @PutMapping("/update")
    public void modifyBook(@RequestBody AddBookRequest request){
        log.info("要修改的饮品: {}",request);
        //1. 先通过id查询出对应的数据信息
        Book book=bookService.getbookById(request.getId());
        //2. 复制前端要修改的属性(将前端传递的值覆盖个drink)
        BeanUtils.copyProperties(request,book);
        //3. 更新数据库
        bookService.updateBook(book);
    }


    /**
     * 删除
     * delete /drink/1
     */
    @DeleteMapping("/{id}")
    public String deleteDrink(@PathVariable Integer id){
        log.info("要删除的饮品id: {}",id);
        bookService.delBook(id);
        //后期使用数据库
        return "删除成功";
    }
    /**
     * 文件上传
     * 前端必须使用html form
     * 前端类型必须设置为 multipart/form-data
     */
    @PostMapping(value = "/upload",consumes ={MediaType.MULTIPART_FORM_DATA_VALUE})
    public String uploadFile(MultipartFile file){
        log.info("参数名 {}", file.getName());
        log.info("上传文件名 {}", file.getOriginalFilename());
        log.info("上传文件大小 {}", file.getSize());

        try {
            String dest="D:\\JavaWeb\\资源上传\\";//文件上传路径
            String destFileName=dest+file.getOriginalFilename();
            File destFile=new File(destFileName);
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("上传失败:{}", e.toString());
            return "上传失败";
        }
        return "上传成功";
    }


}
